/**
  ******************************************************************************
  * @file    acad_core.h
  * @author  Sherlock
  * @version V0.1
  * @date    8/9/2024
  * @brief   Outputs Driver
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef  __ACAD_CORE_H__
#define  __ACAD_CORE_H__

#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief  INCLUDE FILES.
  */
#include "acad_cfg.h"


/*******************************************************************************************************************
                                                核心性能参数定义
*******************************************************************************************************************/
#define EnableFreFilterLevelLimitOpt      /* if all Dot is within range between TraceDCValMin and TraceDCValMax, No frequency */

#define ADCSampleTiValue              200      /* 200us    */
#define ACSamplingRateFre           10000      /* sample rate:10000.00HZ*/
#define ACWaveFormFreMin                7      /* 7.00   HZ*/
#define ACWaveVoltFreMax              500      /* 500.00 HZ*/

#define ACWaveCurrFreMax              500      /* 500.00 HZ*/
#define ACCalLoopFreq              (ACSamplingRateFre/100/ADCSampleNumber)    //ADC计算周期：500Hz（5000Hz采样 / 10点一次），100为频率倍率

#define ACVoltCycleDotNumMax        ((float)ACSamplingRateFre/ACWaveVoltFreMax/2)  //5
#define ACCurrCycleDotNumMax        ((float)ACSamplingRateFre/ACWaveCurrFreMax/2)  //5

#define ACWaveVoltFreThres1           80      /* 100.00 HZ*/
#define ACVoltCycleDotNum1         ((float)ACSamplingRateFre/ACWaveVoltFreThres1/2)  //31.25
#define ACVoltFreThres1CycleNum        2

#define ACWaveVoltFreThres2           150      /* 200.00 HZ*/
#define ACVoltCycleDotNum2         ((float)ACSamplingRateFre/ACWaveVoltFreThres2/2)  //16.66
#define ACVoltFreThres2CycleNum         4

#define ACWaveVoltFreThres3           250      /* 300.00 HZ*/
#define ACVoltCycleDotNum3         ((float)ACSamplingRateFre/ACWaveVoltFreThres3/2)  //10
#define ACVoltFreThres3CycleNum         6

#define ACWaveVoltFreThres4           350      /* 400.00 HZ*/
#define ACVoltCycleDotNum4         ((float)ACSamplingRateFre/ACWaveVoltFreThres4/2)  //7.143
#define ACVoltFreThres4CycleNum         8

#define ACVoltSTRFreqMin               10      /* 10.00Hz  */
#define ACCurrSTRFreqMin               20      /* 20.00Hz  */
#define ACPowrSTRFreqMin               25      /* 25.00Hz  */

#define ACPeriodCalLevel1            2000      /* 20.00    */
#define ACPeriodCalLevel2            7500      /* ???40.00    */

#define ACVoltTraceDCValLower          35      /*  7.86V in 460V*/
#define ACCurrTraceDCValLower          20      /*  48.8mA in 5A */
#define ACSmitFilterFactor              5      /*  xx/5         */

#define DetectMinVoltLimit              3      /* V */
#define DetectMinCurrLimit     ((float)0.01)   /* A */
#define ACNoPassZeroPeriodLimit        75      /* 2*75=150ms(6.67HZ), previous:6 */
#define ACValueUpdatePeriodLimit       80      /* 2*80=160ms(6.25HZ), previous:8 */

#define ACTraceRecordBFTOTNum        6   //一个缓冲中最大过零点数

/*******************************************************************************************************************
                                              切比雪夫滤波器
*******************************************************************************************************************/
/**
 *    b0
 *  ---------------------------
 * 
 */
/* 70-150HZ 5000HZ (Lowpass Chebyshev Order=3) */
// #define ChebyshevCoef_b0        (      668398)
// #define ChebyshevCoef_b1        (     2005194)
// #define ChebyshevCoef_b2        (     2005194)
// #define ChebyshevCoef_b3        (      668398)
// #define ChebyshevCoef_a1        (-28740251832)
// #define ChebyshevCoef_a2        ( 27611406879)
// #define ChebyshevCoef_a3        (- 8865807861)
// #define ChebyshevCoef_Gain        10000000000

/* 100HZ 10000HZ (Lowpass Chebyshev Order=3) */
#define ChebyshevCoef_b0        (       763)
#define ChebyshevCoef_b1        (      2288)
#define ChebyshevCoef_b2        (      2288)
#define ChebyshevCoef_b3        (       763)
#define ChebyshevCoef_a1        (-295950000)
#define ChebyshevCoef_a2        ( 292280000)
#define ChebyshevCoef_a3        (- 96320000)
#define ChebyshevCoef_Gain        100000000

/* 80HZ 10000HZ -3dB (Lowpass Chebyshev Order=3) */
// #define ChebyshevCoef_b0        (       392)
// #define ChebyshevCoef_b1        (      1176)
// #define ChebyshevCoef_b2        (      1176)
// #define ChebyshevCoef_b3        (       392)
// #define ChebyshevCoef_a1        (-296810000)
// #define ChebyshevCoef_a2        ( 293860000)
// #define ChebyshevCoef_a3        (- 97040000)
// #define ChebyshevCoef_Gain        100000000

/* 80HZ 10000HZ -1dB (Lowpass Chebyshev Order=3) */
// #define ChebyshevCoef_b0        (       761)
// #define ChebyshevCoef_b1        (      2283)
// #define ChebyshevCoef_b2        (      2283)
// #define ChebyshevCoef_b3        (       761)
// #define ChebyshevCoef_a1        (-294850000)
// #define ChebyshevCoef_a2        ( 290010000)
// #define ChebyshevCoef_a3        (- 95150000)
// #define ChebyshevCoef_Gain        100000000

/* 70HZ 10000HZ -1dB (Lowpass Chebyshev Order=3) */
// #define ChebyshevCoef_b0        (       511)
// #define ChebyshevCoef_b1        (      1534)
// #define ChebyshevCoef_b2        (      1534)
// #define ChebyshevCoef_b3        (       511)
// #define ChebyshevCoef_a1        (-295510000)
// #define ChebyshevCoef_a2        ( 291260000)
// #define ChebyshevCoef_a3        (- 95750000)
// #define ChebyshevCoef_Gain        100000000

/* 60HZ 10000HZ -1dB (Lowpass Chebyshev Order=3) */
// #define ChebyshevCoef_b0        (       323)
// #define ChebyshevCoef_b1        (       969)
// #define ChebyshevCoef_b2        (       969)
// #define ChebyshevCoef_b3        (       323)
// #define ChebyshevCoef_a1        (-296170000)
// #define ChebyshevCoef_a2        ( 292510000)
// #define ChebyshevCoef_a3        (- 96340000)
// #define ChebyshevCoef_Gain        100000000

/**
  * @brief  切比雪夫滤波器计算
  * @param  LPXn 过去值X记忆序列
  * @param  LPYn 过去值Y记忆序列
  * @return none.
  */
void ACAD_ChebyshevFilter(int64_t *LPXn,int64_t *LPYn,int16_t *StartAddr,int16_t *Area2Addr,uint8_t AddrInterval1,uint8_t AddrInterval2,uint16_t Number);


/*******************************************************************************************************************
                                            过零点检测 Zero pass detect
*******************************************************************************************************************/
/* Struct */
typedef struct
{
	uint8_t recordArea;
	uint8_t recordPosition;
	int16_t lowerVoltage;
	int16_t higherVoltage;
	uint8_t isExist;
	uint8_t isUpThrough;
} Pass0Point;
#define PassPointArrMaxLength 17   //2n+1
typedef struct
{
  uint8_t currentIndex;
  uint8_t periodNum;
  uint8_t syncTraceIndex;
  Pass0Point PassPointArr[PassPointArrMaxLength];
} PassPointMsgType;

/* Macro */
#define ACSignalSta_Pass0            DEF_BIT_00
#define ACSignalSta_MinLevel         DEF_BIT_01
#define ACSignalSta_MaxLevel         DEF_BIT_02

#define ACTraceRecordSta_PassMax     DEF_BIT_00
#define ACTraceRecordSta_PassMin     DEF_BIT_01
/**
  * @brief  过零点计算
  * @param  none
  * @return none.
  */
uint8_t ScanVoltACZeroPassCoordi(uint8_t trace1stFlag,int16_t *CurrAdr,uint8_t AdrGap,uint8_t AdrNum,PassPointMsgType *PassPointMsg,uint8_t *TraceNum,
										uint8_t Area,int16_t Lower,uint8_t *RecordSta,int16_t *RecordMin,int16_t *RecordMax,int16_t *LimitUpper,int16_t *LimitLower);


/*****************************************************************************************************************
                                                弱信号检测
*******************************************************************************************************************/
void CheckInitACTraceRecord(uint8_t P0Status,uint8_t *TraceNum,uint8_t *RecordSta,uint8_t *P0Counter,uint8_t *LVStatus,uint8_t *LVCounter,int16_t Lower,
														int16_t *RecordMin,int16_t *RecordMax,int16_t *LimitUpper,int16_t *LimitLower,void (*ResetFunc)(void));


/*****************************************************************************************************************
                                                计算频率
*******************************************************************************************************************/
/**
  * @brief  计算频率函数
  * @param  TraceNum 当前缓冲区的过零点数
  * @param  GlobalFre 全局频率
  * @return 频率计算结果.
  */
float CalVoltFreFromTraceRecord(PassPointMsgType *PassPointMsg,uint8_t TraceNum,float GlobalFre);


/*****************************************************************************************************************
                                            计算周期起始点
*******************************************************************************************************************/
/**
  * @brief  获取周期信息
  * @param  StartAddr 当前缓冲区的最新点
  * @param  DotNum 周期点数
  * @param  CycleTi 周期精确点数
  * @param  RemainTi 上一个过零点残留值
  * @param  CurrPointTi 当前过零点残留值
  * @return 频率计算结果.
  */
int16_t *GetACCycleViewMSG(PassPointMsgType *PassPointMsg,int16_t *StartAddr,uint16_t *DotNum,float *CycleTi,float *RemainTi,float *CurrPointTi);


/*****************************************************************************************************************
                                            计算相电压
*******************************************************************************************************************/
float CalADCVoltVirtualPhaseValue(int16_t *StartAddr,uint8_t AddrInterval,uint16_t Number,float CycleTi,float RemainTi,float CurrPointTi);


/*****************************************************************************************************************
                                            计算线电压
*******************************************************************************************************************/
float CalADCVirtualLineValue(int16_t *L1StartAddr,int16_t *L2StartAddr,uint8_t AddrInterval,uint16_t Number,float CycleTi,float RemainTi,float CurrPointTi,float CalibL1,float CalibL2);


/*****************************************************************************************************************
                                            计算相角
*******************************************************************************************************************/
float CalPhaseFromTraceRecord(PassPointMsgType *MainPassPointMsg, PassPointMsgType *GensPassPointMsg);


/*****************************************************************************************************************
                                            计算有功功率
*******************************************************************************************************************/
float CalADCPowerValue(int16_t *UStartAddr,int8_t IAdrInterval,uint8_t AddrInterval,uint16_t Number,float CycleTi,float RemainTi,float CurrPointTi);


/*****************************************************************************************************************
                                            计算无功功率
*******************************************************************************************************************/
float CalADCPowerQValue(int16_t *UStartAddr,int8_t IAdrInterval,int8_t AddrInterval,uint16_t Number,float CycleTi,float RemainTi, float CurrPointTi);


/*****************************************************************************************************************
                                          根据功率因数计算相角
*******************************************************************************************************************/
float CalPhaseFromPowerFactor(float activePower, float reactivePower, float apparentPower);


/*****************************************************************************************************************
                                          同步合闸误差计算函数
*******************************************************************************************************************/
#define GovSync_MaxPassZeroPoint   10
float CalGovSyncErr(int8_t *SyncTraceLevelMode, uint8_t *SyncTrace1stSta, float *SyncLastErr, PassPointMsgType *GensPassPointMsg, PassPointMsgType *LoadPassPointMsg);


#ifdef __cplusplus
}
#endif



#endif   /*  __ACAD_CORE_H__  */

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/